﻿$(function() {
    AgentUpdate.init();
    Utils.textBoxsOnEnter(AgentUpdate.Update);
});


var AgentUpdate =
{
    init: function() {
        $("#btnSave").click(this.Update);
        $("#username").focus();
        $('input[name="supportedProds"]').change(function() { AgentUpdate.SwitchDisabled($(this)); });
    }
    , Update: function() {
        var username = $("#username");
        var creditlimit = $("#creditlimit");
        var possess = $("#possess");

        targetFunction = (parent != undefined) ? parent.ErrorHelper : ErrorHelper;
        targetFunction.checkNonNull(username, m.Err0203);
        targetFunction.checkNonNull(creditlimit, m.Err0204);
        targetFunction.checkNonNull(possess, m.Err0207);

        $('input[name="supportedProdsPrice"]').each(
            function() {
                if (!this.disabled) {
                    if (this.value.length > 0) {
                        if (isNaN(this.value)) {
                            var msg = $(this).parent().children().text() + m.Err9004;
                            targetFunction.setError(msg, $(this));
                        }
                    } else {
                        var msg = $(this).parent().children().text() + m.Err9004;
                        targetFunction.setError(msg, $(this));
                    }
                }
            }
        );

        if (!targetFunction.popUpIfError()) {
            var param = $("#MAUpdate").serialize();
            var cb = Utils.getCBfunction('Agent', '');
            var ecb = function() { $("#username").focus(); };
            utility.ajaxext("AgentService/UpdateMA", param, "POST", cb, ecb);
        }
    }
    , SwitchDisabled: function(obj) {
        var target = obj.parent().parent().children()[0];
        target.disabled = !obj[0].checked;
        if (!target.disabled) {
            target.focus();
        }
    }
};
